package com.zhkj.shixi.tvfilemanager.view;

import android.content.Context;
import android.graphics.Rect;
import android.util.Log;
import android.view.View;
import android.widget.AdapterView;
import android.widget.ImageView;
import android.widget.ListView;
import android.widget.TextView;

import com.open.androidtvwidget.bridge.EffectNoDrawBridge;
import com.open.androidtvwidget.view.MainUpView;
import com.zhkj.shixi.tvfilemanager.FileListActivity;
import com.zhkj.shixi.tvfilemanager.FileManager;
import com.zhkj.shixi.tvfilemanager.FileMenuCtrl;
import com.zhkj.shixi.tvfilemanager.FileSearch;
import com.zhkj.shixi.tvfilemanager.StaticClasses;
import com.zhkj.shixi.tvfilemanager.adapter.GridViewAdapter;
import com.zhkj.shixi.tvfilemanager.adapter.ListViewAdapter;
import com.zhkj.shixi.tvfilemanager.base.BasePager;
import com.zhkj.shixi.tvfilemanager.R;
import com.zhkj.shixi.tvfilemanager.ViewEntity;
import com.zhkj.shixi.tvfilemanager.utils.DateUtil;

import java.io.File;
import java.util.ArrayList;
import java.util.List;

import static com.zhkj.shixi.tvfilemanager.StaticClasses.deviceList;
import static com.zhkj.shixi.tvfilemanager.StaticClasses.fileManager;
import static com.zhkj.shixi.tvfilemanager.view.GrideViewPager.bMap;
import static com.zhkj.shixi.tvfilemanager.view.GrideViewPager.fileList;
import static com.zhkj.shixi.tvfilemanager.view.GrideViewPager.mIsOver;

/**
 * Created by nieshuang on 19-8-13.
 */

public class ListViewPager extends BasePager{
    private static ListViewAdapter listViewAdapter;
    private static ArrayList<ViewEntity> mArrayList = new ArrayList<>();
    private static ListView mListView;
    private FileListActivity mActivity;

    private static MainUpView mMainUpView;
    private static View mOldView;

    private int mSavePos = 0;
    private static EffectNoDrawBridge mBridget;
    private final String TAG = "ListViewPager";


    public ListViewPager(Context context, int resource) {
        super(context, resource);
        mActivity = (FileListActivity) context;
    }

    @Override
    public View initView(int resource) {
        View view = View.inflate(context,resource,null);

        mListView = (ListView) view.findViewById(R.id.list_view);
        mMainUpView = (MainUpView) view.findViewById(R.id.mainUpView_list);
        onFocusMove();
        return view;
    }

    public void callBack(){
        mIsOver=true;
        Log.i(TAG, "run: "+GrideViewPager.fileSearch.getAllMediaFiles());
    }
    @Override
    public void initData(){
        super.initData();
        if(deviceList .isEmpty()) {
            Log.e("Device","没有设备");
            return;
        }
        if(fileList==null)fileList = deviceList;
        listViewAdapter= new ListViewAdapter(context,mArrayList);
        flushViewData(fileList);
        mListView.setOnFocusChangeListener(new View.OnFocusChangeListener() {
            @Override
            public void onFocusChange(View v, boolean hasFocus) {
                if (!mListView.hasFocus()){
                    mBridget.setVisibleWidget(true);
                }else{
                    mBridget.setVisibleWidget(false);
                    mListView.setSelection(0);
                    mListView.requestFocus();
                }
            }
        });
        mListView.setOnItemClickListener(new AdapterView.OnItemClickListener() {
            @Override
            public void onItemClick(AdapterView<?> parent, View view, int position, long id) {

                Log.e(TAG,"===============4 1");
                // mFindhandler.removeCallbacksAndMessages(null);
                mSavePos = position; // 保存原来的位置
                Log.e(TAG,"===============5");

                listViewAdapter.notifyDataSetChanged();

                mListView.postDelayed(new Runnable() {
                    @Override
                    public void run() {

                        if (mListView.getCount() != 0){
                            Log.e("cnk","===============a 6");
                            mBridget.setVisibleWidget(false);
                            mListView.setSelection(0);
                            mListView.requestFocus();
                        }else{
                            mBridget.setVisibleWidget(true);
                        }
                    }
                },50);
                Log.e(TAG,"===============6");

                ImageView listSelected = view.findViewById(R.id.listSelected);
                String tPath="";
                Thread thread=null;
                if (fileManager.getCurrentURL() == null) {
                    fileManager.setCurrentURL(fileList.get(position).getAbsolutePath());
                    if(!tPath.equals(fileManager.getCurrentURL())){
                        if(mIsOver){
                            mIsOver=false;
                            GrideViewPager.fileSearch.getAllMediaFiles().clear();
                        }

                        thread =new Thread(new Runnable() {
                            @Override
                            public void run() {
                                String rootUrl = fileManager.getCurrentURL();
                                Log.w("URLS",fileManager.getCurrentURL());
                                GrideViewPager.fileSearch.fileFilterF(rootUrl);
                                callBack();
                            }
                        });
                        thread.start();
                    }
                    tPath=fileManager.getCurrentURL();
                    fileManager.setmUSB(position);
                    TextView textView = mActivity.findViewById(R.id.returnText);
                    textView.setText(textView.getText() + " " + fileList.get(position).getName());
                }
                //fileList = fileManager.getTypeFiles(fileManager.getCurrentURL(),fileManager.getType());
                FileMenuCtrl fileCtrl = mActivity.getFileMenuCtrl();
                File file = fileList.get(position);
                if (mActivity.isSlctStat()) {
                    if (listSelected.getVisibility() == View.INVISIBLE) {
                        listSelected.setVisibility(View.VISIBLE);
                        fileCtrl.addFile(file);
                    } else {
                        listSelected.setVisibility(View.INVISIBLE);
                        fileCtrl.removeFile(file);
                    }
                    bMap.put(position,listSelected.getVisibility());
                } else {
                    fileCtrl.clickFile(file);
                    if (!FileListActivity.searchMode) {
                        fileList = fileManager.getTypeFiles(fileManager.getCurrentURL(), fileManager.getType());
                        flushViewData(fileList);
                        bMap.clear();
                    }
                }

            }
        });

        /*Log.d("list111","list111 ==================size =" + mArrayList.size());
        ListViewAdapter listViewAdapter = new ListViewAdapter(context,mArrayList);
        mListView.setAdapter(listViewAdapter);

        mListView.setOnItemSelectedListener(new AdapterView.OnItemSelectedListener() {
            @Override
            public void onItemSelected(AdapterView<?> parent, View view, int position, long id) {
                if (view != null) {
                    view.bringToFront();
                    mMainUpView.setFocusView(view, mOldView, 1.0f);
                    mOldView = view;
                }
            }

            @Override
            public void onNothingSelected(AdapterView<?> parent) {
            }
        });*/
    }

    private static String sizeFormat(Long size){
        int count=0;
        String unit[]={"Byte","KB","MB","GB","TB","PB","ERROR"};
        while(size>=1024){
            size/=1024;
            count++;
        }
        return size+unit[count];
    }
    public static void flushViewData(List<File> fileList){
        mArrayList.clear();
        int i =0 ;
        for (File file:fileList) {
            ViewEntity viewEntity = new ViewEntity();
            viewEntity.setmFile(file);
            if (file.isDirectory()){
                if(file.getParent().equals(deviceList.get(i).getParent())) {
                    viewEntity.setmImageId(R.drawable.audio_icn_normal2);
                    i++;
                }
                else {
                    viewEntity.setmImageId(R.drawable.audio_usb_icn_folder_normal);
                }
            }else {
                if (fileManager.getType() == FileManager.MUSIC_TYPE) {
                    viewEntity.setmImageId(R.drawable.audio_usb_icn_audio_normal);
                } else if (fileManager.getType() == FileManager.VIDEO_TYPE) {
                    viewEntity.setmImageId(R.drawable.video_usb_icn_video_normal);
                } else if (fileManager.getType() == FileManager.IMAGE_TYPE) {
                    viewEntity.setmImageId(R.drawable.video_usb_icn_img_pressed);
                }
            }
            viewEntity.setmFileName(file.getName());
            viewEntity.setmFileSize(sizeFormat(file.length()));
            viewEntity.setmTime("|"+DateUtil.stampToTime(file.lastModified()));
            mArrayList.add(viewEntity);
        }
        listViewAdapter.setmArrayList(mArrayList);
        mListView.setAdapter(listViewAdapter);

        mListView.setOnItemSelectedListener(new AdapterView.OnItemSelectedListener() {
            @Override
            public void onItemSelected(AdapterView<?> parent, View view, int position, long id) {
                if (view != null) {
                    view.bringToFront();
                    mMainUpView.setFocusView(view, mOldView, 1.0f);
                    mOldView = view;
                }
                if (mListView.getCount() != 0){
                    mBridget.setVisibleWidget(false);
                }
            }

            @Override
            public void onNothingSelected(AdapterView<?> parent) {
            }
        });
    }

    private void onFocusMove(){
        mMainUpView.setEffectBridge(new EffectNoDrawBridge());
        mBridget = (EffectNoDrawBridge) mMainUpView.getEffectBridge();
        mBridget.setTranDurAnimTime(150);
        // 设置移动边框的图片.
        mMainUpView.setUpRectResource(R.drawable.white_light_10);
        // 移动方框缩小的距离.
        mMainUpView.setDrawUpRectPadding(new Rect(10, 10, 10, 10));
    }
}
